﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class list_event : System.Web.UI.Page
    {
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        MailConfigBUS mailConfigBus = null;
        MailGroupBUS mailGroupBus = null;
        EventBUS eventBus = null;
        SignatureBUS signBus = null;
        DataTable group = null;
        MailGroupBUS mgBUS = new MailGroupBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    //txtFromDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    //txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    loadData();
                }
                catch (Exception)
                {

                }
            }
        }


        private void InitialBUS()
        {
            mailConfigBus = new MailConfigBUS();
            mailGroupBus = new MailGroupBUS();
            eventBus = new EventBUS();
        }
        private void createTableMail()
        {
            group = new DataTable("group");
            DataColumn Id = new DataColumn("Id");
            Id.DataType = System.Type.GetType("System.Int32");
            DataColumn Name = new DataColumn("Name");
            DataColumn[] key = { Id };
            group.Columns.Add(Id);
            group.Columns.Add(Name);
            group.PrimaryKey = key;
        }

        private void loadData()
        {

            try
            {

                DataTable MailGroup = new DataTable();
                if (Session["us-login"] != null)
                {
                    if (getUserLogin().DepartmentId == 1)
                    {
                        MailGroup = mgBUS.GetAllNew();
                    }
                    else
                    {
                        MailGroup = mgBUS.GetMailGroupByUserId(getUserLogin().UserId);
                    }
                    DataRow dr = MailGroup.NewRow();
                    dr["Name"] = "Tất cả";
                    dr["Id"] = "-1";
                    MailGroup.Rows.InsertAt(dr, 0);
                    this.drlMailGroup.DataSource = MailGroup;
                    this.drlMailGroup.DataTextField = "Name";
                    this.drlMailGroup.DataValueField = "Id";
                    this.drlMailGroup.DataBind();
                }

            }
            catch (Exception ex)
            {

                logs.Error(userLogin.Username + "ListEvent - LoadData", ex);

            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");//test confict
            return null;
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {


        }


        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {

            try
            {
                InitialBUS();
                int eventId = int.Parse(((ImageButton)sender).CommandArgument.ToString());
                ConnectionData.OpenMyConnection();
                eventBus.tblEvent_Delete(eventId);
                visibleMessage(false);
                pnSuccess.Visible = true;
                lblSuccess.Text = "Bạn vừa xóa thành công sự kiện !";
                ConnectionData.CloseMyConnection();

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "-ListEvent - btnDelete_Click", ex);
                pnError.Visible = true;
                lblError.Text = ex.Message;
            }
        }

        protected void visibleMessage(bool vis)
        {
            pnError.Visible = vis;
            pnSuccess.Visible = vis;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                EventBUS eventBus = new EventBUS();
                int eventId = id;
                eventBus.tblEvent_Delete(eventId);

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "-ListEvent - btnDelete_Click", ex);
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<SearchResult> Search(int groupid, string subject, string from, string to)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            EventBUS ctBUS = new EventBUS();
            DataTable customerBySelect = ctBUS.Search(userLogin.UserId, groupid, subject, from, to);
            List<SearchResult> result = new List<SearchResult>();
            SearchResult item = new SearchResult();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                item = new SearchResult();
                item.EventId = Convert.ToInt32(dr["EventId"] + "");
                item.Sender = dr["Sender"] + "";
                item.GroupName = dr["GroupName"] + "";
                item.Subject = dr["Subject"] + "";
                if (dr["StartDate"] + "" != "")
                    item.StartDate = Convert.ToDateTime(dr["StartDate"] + "");
                if (dr["EndDate"] + "" != "")
                    item.EndDate = Convert.ToDateTime(dr["EndDate"] + "");
                result.Add(item);
            }

            return result;
        }
    }
    public class SearchResult
    {
        public int EventId { get; set; }
        public int groupid { get; set; }
        public string Sender { get; set; }
        public string GroupName { get; set; }
        public string Subject { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }

}